<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="角色名称">
              <a-input v-model="queryParam.roleName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="角色代码">
              <a-input v-model="queryParam.roleCode" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button type="primary"@click="() => {this.init()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.roleAddOrUpdate.edit('','add')">新建</a-button>
    </div>
    <a-table
      :columns="columns"
      :dataSource="tableData"
      :loading="loading"
      :pagination="pagination"
      :rowKey="record => record.roleId"
      @change="handleTableChange"
    >
    </a-table>
    <role-add-or-update
      ref="roleAddOrUpdate"
      @ok="handleOk"
    />
    <role-perm-modal
      ref="rolePermModal"
    />
  </a-card>

</template>

<script>
import { fetchList, delObj } from '@api/sys/role'
import RoleAddOrUpdate from './roleAddOrUpdate'
import RolePermModal from './rolePermModal'
const dsTypeList = [
  {
    label: '全部',
    value: '0'
  },
  {
    label: '自定义',
    value: '1'
  },
  {
    label: '本级',
    value: '2'
  },
  {
    label: '本级及子级',
    value: '3'
  }

]
export default {
  name: 'Role',
  components: {
    RoleAddOrUpdate,
    RolePermModal
  },
  data () {
    return {
      loading: false,
      columns: [
        {
          title: '#',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        },
        {
          title: 'ID',
          dataIndex: 'roleId',
          sorter: true
        },
        {
          title: '角色代码',
          dataIndex: 'roleCode'
        },
        {
          title: '角色名称',
          dataIndex: 'roleName'
        },
        {
          title: '数据权限',
          dataIndex: 'dsType',
          customRender: (record) => {
            return (
              <div>
                {
                  dsTypeList.map(item => {
                    if (item.value === record) {
                      return <a-tag color="#108ee9" >{ item.label } </a-tag>
                    }
                  })
                }
              </div>
            )
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          width: '30%',
          customRender: (text, record, index) => {
            return (
              <div>
                <a onClick={(e) => this.edit(record)}
                >修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="是否删除角色" onConfirm={(e) => this.delete(record)} okText="是" cancelText="否">
                  <a-icon slot="icon" type="question-circle-o" style="color: red" />
                  <a href="javascript:;" style="color: red">删除</a>
                </a-popconfirm>
                <a-divider type="vertical" />
                <a href="javascript:;"
                  onClick={(e) => this.show(record)}
                >查看</a>
                <a-divider type="vertical" />
                <a href="javascript:;"
                  onClick={(e) => this.perms(record)}
                >权限</a>
              </div>
            )
          }
        }
      ],
      tableData: [],
      pagination: {
        pageSize: 10,
        current: 1,
        total: 0,
        showSizeChanger: true
      },
      pages: {
        size: 10,
        current: 1
      },
      queryParam: {
        roleName: '',
        roleCode: ''
      }
    }
  },
  methods: {
    init () {
      this.loading = true
      fetchList({
        ...this.queryParam,
        ...this.pages
      }).then(res => {
        this.tableData = res.data.records
        this.pagination.total = res.data.total
        this.loading = false
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.pages.size = pagination.pageSize
      this.pages.current = pagination.current
      this.init()
    },
    edit (record) {
      console.log(record, 'edit')
      this.$refs.roleAddOrUpdate.edit(record.roleId, 'edit')
    },
    delete (record) {
      delObj(record.roleId).then(res => {
        this.$message.info('删除成功')
        this.init()
      })
    },
    show (record) {
      this.$refs.roleAddOrUpdate.edit(record.roleId, 'show')
    },
    handleOk () {
      this.init()
    },
    perms (record) {
      this.$refs.rolePermModal.init(record)
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
